#pragma once
#include "../widgets.h"
#include <QWidget>
#include <QStyledItemDelegate>
#include <QStandardItemModel>
#include <QTimer>
#include <QLabel>
#include <map>
#include <vector>

const int DataIdRole = Qt::UserRole + 1;
const int PrefixImgRole = Qt::UserRole + 2;
const int PostfixImgRole = Qt::UserRole + 3;
const int PrefixTipRole = Qt::UserRole + 4;
const int PostfixTipRole = Qt::UserRole + 5;
const int HeaderSortRole = Qt::UserRole + 6;

struct FlashInfo
{
	QString				id;
	bool				entireRow = true;
	std::vector<int>	cols;
	int					flashCount = -1;

	FlashInfo() = default;
	FlashInfo(const QString& str, bool e = true, std::vector<int> s = std::vector<int>(), int c = -1)
		:id(str), entireRow(e), cols(s), flashCount(c)
	{

	}
};

class QTableView;
class QScrollBar;
class QAbstractItemDelegate;
class S_WIDGETS_EXPORT DefaulTableItemDelegate : public QStyledItemDelegate
{
public:
	DefaulTableItemDelegate(QObject* p);
	virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};

class S_WIDGETS_EXPORT SVirtualTable : public QWidget
{
	Q_OBJECT
public:
	SVirtualTable(QWidget* p);

	QTableView* table() const { return m_table; }
	void setRealDataCount(int c);
	int pageCount() const { return m_pageCount; }
	int sliderPos() const;
	void resetScroll(bool resetPos = false);
	bool isVScrollBarShown();
	void addFlashInfo(const FlashInfo& fi, bool startImmediately = true);
	void removeFlashInfo(const FlashInfo& fi);
	void clearTable();
	void startFlash();

	void setHeaderSortable(bool v);
	void lockTable();
	void unlockTable();
	void showLockNewData(const QString& tip);

protected:
	bool eventFilter(QObject* watched, QEvent* event) override;
	void resizeEvent(QResizeEvent* event) override;

signals:
	void viewPosChanged(int position); //滚动条位置改变
	void headerSort(int col);
	void unlocked();

protected slots:
	void onTimeOut();
	void onHeaderClicked(int logicalIndex);
	void updateLockGeo();

private:
	QTableView* m_table = nullptr;
	QScrollBar* m_scrollBar = nullptr;//垂直滚动条，水平滚动条使用TableView自己的
	QLabel* m_lockIndicator = nullptr;
	int m_realDataCount = 0;
	int m_pageCount = 0;
	QTimer* m_flashTimer = nullptr;
	bool    m_headerSortable = false;
	bool	m_locked = false;
};

class S_WIDGETS_EXPORT DefaultTableModel : public QStandardItemModel
{
public:
	DefaultTableModel(QObject* p);
	void setTable(SVirtualTable* t) { m_table = t; }
	QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
	void addFlashInfo(const FlashInfo& fi);
	void removeFlashInfo(const FlashInfo& fi);
	bool flash();

private:
	SVirtualTable* m_table = nullptr;
	std::map<QString, FlashInfo>	m_flashInfos; //key 为其他类型?
};